//
//  CaseManagerViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
/*
 BBS
 */

#import "CaseManagerViewController.h"
#import "BBSAddCaseController.h"
#import "BBSAddCaseModel.h"
#import "BBSCaseManagerHeaderView.h"
#import "BBSAddCaseCell.h"

#define kPadding 20
//本地存储的路径
#define kPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"case.plist"]



@interface CaseManagerViewController () <UITableViewDelegate, UITableViewDataSource>

//病人个人信息的容器view
@property (weak, nonatomic) UIView *containerView;

//整个containerView最下面的线
@property (weak, nonatomic) UIView *lineView;

//添加病例button
@property (weak, nonatomic) UIButton *addCaseBtn;

@property (weak, nonatomic) UIView *footerView;

//整页的tableView属性
@property (weak, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *modelArray;



@end






@implementation CaseManagerViewController

-(NSMutableArray *)modelArray{
    
    if (!_modelArray) {
        
        _modelArray = [[NSMutableArray alloc] init];
    }
    return  _modelArray;
}

//页面将要显示的时候, 首先加载存储的数据
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    ;
    [self loadLocalData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

- (void)loadLocalData{

    NSArray *arr = [NSArray  arrayWithContentsOfFile:kPath];
    //TODO:
    [self.modelArray removeAllObjects];
    
    for (int i = 0; i < arr.count; i++) {
        //把读出来的二进制数据赋值给模型
        BBSAddCaseModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:arr[i]];
        
        [self.modelArray addObject:model];
    }
    [self.tableView reloadData];
}



- (void)setupUI{
    
    self.navigationController.navigationBar.translucent = NO;
    [self addSubViews];
    
}

//添加子控件
- (void)addSubViews{
    
    BBSCaseManagerHeaderView *containerView = [[BBSCaseManagerHeaderView alloc] init];
    
    self.containerView = containerView;
    
    [self.view addSubview:containerView];

    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(self.view);
        
        make.height.equalTo(180);
        
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.containerView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.left.equalTo(containerView);
        
        make.top.equalTo(containerView.mas_bottom).offset(-10);
        
        make.height.equalTo(1);
        
    }];

    
    UIView *footerView = [[UIView alloc] init];
    [self.view addSubview:footerView];
    footerView.backgroundColor = [UIColor whiteColor];
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        
        make.height.equalTo(64);
        
    }];
    
    UIButton *addCaseBtn = [[UIButton alloc] init];
    [addCaseBtn setBackgroundColor:kThemeColor];
    [addCaseBtn setTitle:@"添加病例" forState:UIControlStateNormal];
    [addCaseBtn.titleLabel setFont:FONT(18)];
    [addCaseBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [footerView addSubview:addCaseBtn];
    //addCaseBtn添加点击事件
    [addCaseBtn addTarget:self action:@selector(addCaseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [addCaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.equalTo(footerView).offset(15);
        
        make.right.bottom.equalTo(footerView).offset(-15);

    }];
    
    
    UITableView *tableView = [[UITableView alloc]init];
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(containerView.mas_bottom);
        
        make.left.right.equalTo(self.view);
        
        make.bottom.equalTo(footerView.mas_top);
        
    }];
    
    [tableView registerClass:[BBSAddCaseCell class] forCellReuseIdentifier:@"cell1"];
    
    [tableView registerClass:[BBSAddCaseCell class] forCellReuseIdentifier:@"cell2"];
    
    tableView.dataSource = self;
    
    tableView.rowHeight = 150;
    
    
}



//添加病例addCaseBtn 点击事件
- (void)addCaseBtnClick:(UIButton *)btn{
    
    BBSAddCaseController *addCaseController = [[BBSAddCaseController alloc] init];
    
    [self.navigationController pushViewController:addCaseController animated:YES];
    
}


#pragma mark - UITableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BBSAddCaseCell *cell;
    
    BBSAddCaseModel *model = self.modelArray[indexPath.row];
    
    
    if (model.imageData.length == 0) {
    
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
    }
    
    cell.model = model;
    
    return cell;
}




@end
